package com.zhangyj.impl.actions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.zhangyj.LocalMessage;
import com.zhangyj.action.AbstractAction;
import com.zhangyj.action.RequestAction;
import com.zhangyj.component.grid.HTMLTable;
import com.zhangyj.component.grid.model.ITableModel;
import com.zhangyj.dao.ClientDao;
import com.zhangyj.dao.DaoManager;
import com.zhangyj.db.DBUtil;
import com.zhangyj.entitis.Client;
import com.zhangyj.entitis.ClientType;
import com.zhangyj.entitis.SystemParam;
import com.zhangyj.impl.UserContext;
import com.zhangyj.impl.ViewName;
import com.zhangyj.impl.component.grid.DefaultHTMLTable;
import com.zhangyj.impl.component.grid.DefaultTableMode;
import com.zhangyj.impl.component.grid.render.HyperLinkCellRender;
import com.zhangyj.impl.component.type.GridCol;
import com.zhangyj.impl.component.type.HyperLink;
import com.zhangyj.security.Permission;
import com.zhangyj.util.Pagination;
import com.zhangyj.util.Tools;
@RequestAction(name="client")
public class ClientAction extends AbstractAction {
	private static final long serialVersionUID = 3064456089676261177L;


	public String tplist() throws Exception {
		this.validatePermission(Permission.VIEW);
		SystemParam systemParam = SystemParam.getSystemParam(request);
		ClientDao dao =  DaoManager.getDao(ClientDao.class);
		List<ClientType> list = dao.getTypes();
		List<Object[]> mlist = new ArrayList<Object[]>();
		String contextPath = request.getContextPath();
		for (Iterator<ClientType> iterator = list.iterator(); iterator.hasNext();) {
			ClientType pt = iterator.next();
			Object[] arr = new Object[3];
			arr[0] = pt.getHylm();
			arr[1] = new HyperLink(contextPath + "/client/tpedit.do?" + systemParam.toUrlParam() + "&hylh=", pt.getHylh(), LocalMessage.get("edit"), "_self");
			arr[2] = new HyperLink(contextPath + "/client/tpdel.do?" + systemParam.toUrlParam() + "&hylh=", pt.getHylh(), LocalMessage.get("del"), "_self");
			mlist.add(arr);
		}
		ITableModel tm = new DefaultTableMode(mlist.iterator(), new GridCol[] { new GridCol("客户类名", 0), new GridCol(LocalMessage.get("edit")), new GridCol(LocalMessage.get("del")) });
		HTMLTable ht = new DefaultHTMLTable(tm);
		ht.setDefaultRenderer(HyperLink.class, new HyperLinkCellRender());
		request.setAttribute("table", ht);
		return ViewName.CLIENT_TYPE_LIST;
	}

	public String tpedit() throws Exception {
		this.validatePermission(Permission.UPDATE);
		String cplh = getParameter( "hylh");
		ClientDao dao = DaoManager.getDao(ClientDao.class);
		ClientType ct = dao.loadType(cplh);
		request.setAttribute("ct", ct);
		return ViewName.CLIENT_TYPE_EDIT;
	}

	public String tpadd() throws Exception {
		this.validatePermission(Permission.ADD);
		ClientType ct = new ClientType(getParameter( "hylh"), getParameter( "hylm"));
		request.setAttribute("ct", ct);
		return ViewName.CLIENT_TYPE_ADD;
	}

	public String tpdel() throws Exception {
		this.validatePermission(Permission.DELETE);
		String hylh = getParameter( "hylh");
		ClientDao dao = DaoManager.getDao(ClientDao.class);
		dao.delType(hylh);
		return tplist();
	}

	public String tpsave() throws Exception {
		ClientType pt = new ClientType(getParameter( "hylh"), getParameter( "hylm"));
		boolean isUpdate = true;
		if (pt.getHylh() == null || pt.getHylh().length() == 0) {
			isUpdate = false;
			pt.setHylh(DBUtil.getStringId("kehufl", "HY", 10));
		}
		if (isUpdate) {
			this.validatePermission(Permission.UPDATE);
		} else {
			this.validatePermission(Permission.ADD);
		}
		ClientDao dao = DaoManager.getDao(ClientDao.class);
		dao.saveType(pt, isUpdate);
		return tplist();
	}

	public String list() throws Exception {
		this.validatePermission(Permission.VIEW);
		SystemParam systemParam = SystemParam.getSystemParam(request);
		String con = getParameter( "con");
		Pagination pagination = new Pagination(request);
		ClientDao dao = DaoManager.getDao(ClientDao.class);
		pagination.setTotalCount(dao.getClientCount(con));
		List<Client> list = dao.getClientList(pagination, con);

		List<Object[]> mlist = new ArrayList<Object[]>();
		String contextPath = request.getContextPath();
		for (Iterator<Client> iterator = list.iterator(); iterator.hasNext();) {
			Client client = iterator.next();
			Object[] arr = new Object[12];
			int i = 0;
			arr[i++] = client.getKhxm();
			arr[i++] = client.getLxdh();
			arr[i++] = client.getZhye();
			arr[i++] = client.getJfs();
			arr[i++] = client.getKhdz();
			arr[i++] = client.getLrryname();
			arr[i++] = client.getKhzyname();
			arr[i++] = new HyperLink(contextPath + "/client/view.do?" + systemParam.toUrlParam() + "&khbh=", client.getKhbh(), LocalMessage.get("view"), "_self");
			arr[i++] = new HyperLink(contextPath + "/client/edit.do?" + systemParam.toUrlParam() + "&khbh=", client.getKhbh(), LocalMessage.get("edit"), "_self");
			arr[i++] = new HyperLink(contextPath + "/client/fpzy.do?" + systemParam.toUrlParam() + "&khbh=", client.getKhbh(), "分配专员", "_self");
			arr[i++] = new HyperLink(contextPath + "/client/del.do?" + systemParam.toUrlParam() + "&khbh=", client.getKhbh(), LocalMessage.get("del"), "_self");
			arr[i++] = new HyperLink(contextPath + "/jt/add.do?" + systemParam.toUrlParam() + "&khbh=", client.getKhbh(), "交流", "_self");

			mlist.add(arr);
		}
		GridCol[] head = new GridCol[] { new GridCol("客户名", 0), new GridCol("联系电话", 60), new GridCol("账户余额", 70), new GridCol("积分数"), new GridCol("地址", 0), new GridCol("录入人员", 0),
				new GridCol("客户专员", 0), new GridCol(LocalMessage.get("view")), new GridCol(LocalMessage.get("edit")), new GridCol("分配专员", 100), new GridCol(LocalMessage.get("del")), new GridCol("交流") };
		ITableModel tm = new DefaultTableMode(mlist.iterator(), head);
		HTMLTable ht = new DefaultHTMLTable(tm);
		ht.setDefaultRenderer(HyperLink.class, new HyperLinkCellRender());
		request.setAttribute("table", ht);
		StringBuilder url = new StringBuilder();
		url.append("/client/list.do");
		if (con != null) {
			url.append("?con=").append(con);
		}
		pagination.setUrl(url.toString());
		return ViewName.CLIENT_LIST;
	}

	public String add() throws Exception {
		this.validatePermission(Permission.ADD);
		Client client = new Client();
		Tools.fullBean(client, request);
		ClientDao dao = DaoManager.getDao(ClientDao.class);
		List<ClientType> list = dao.getTypes();
		request.setAttribute("client", client);
		request.setAttribute("tplist", list);
		return ViewName.CLIENT_ADD;
	}

	public String edit() throws Exception {
		this.validatePermission(Permission.UPDATE);
		ClientDao dao = DaoManager.getDao(ClientDao.class);
		String khbh = getParameter( "khbh");
		List<ClientType> list = dao.getTypes();
		Client client = dao.loadClient(khbh);
		request.setAttribute("client", client);
		request.setAttribute("tplist", list);
		return ViewName.CLIENT_EDIT;
	}

	public String del() throws Exception {
		this.validatePermission(Permission.DELETE);
		String khbh = getParameter( "khbh");
		ClientDao dao = DaoManager.getDao(ClientDao.class);
		dao.del(khbh);
		return list();
	}

	public String view() throws Exception {
		this.validatePermission(Permission.VIEW);
		ClientDao dao = DaoManager.getDao(ClientDao.class);
		String khbh = getParameter( "khbh");
		Client client = dao.loadClient(khbh);
		request.setAttribute("client", client);
		return ViewName.CLIENT_VIEW;
	}

	public String save() throws Exception {
		String khbh = getParameter( "khbh");
		ClientDao dao = DaoManager.getDao(ClientDao.class);
		boolean isUpdate = true;
		Client client = null;
		if (khbh.length() == 0) {
			isUpdate = false;
			client = new Client();
			client.setKhbh(DBUtil.getStringId("kehu", "KH", 20));
			client.setLrry(UserContext.getUserContext(request).getUserid());
		} else {
			client = dao.loadClient(khbh);
		}
		if (isUpdate) {
			this.validatePermission(Permission.UPDATE);
		} else {
			this.validatePermission(Permission.ADD);
		}
		Tools.fullBean(client, request);
		dao.save(client, isUpdate);
		return list();
	}


	public String fpsave() throws Exception {
		this.validatePermission(Permission.UPDATE);
		String khbh = getParameter( "khbh");
		String userid = getParameter( "userid");
		if (khbh.length() > 0 && userid.length() > 0) {
			ClientDao dao = DaoManager.getDao(ClientDao.class);
			dao.fpzy(khbh, Integer.valueOf(userid));
		}
		return list();
	}

}
